from rest_framework import mixins, generics
from rest_framework.views import APIView

from .serializers import MerchantSerializer
from meituan.models import Merchant


class MerchantView(
    mixins.CreateModelMixin,  ## 提供处理 Post 请求的方法
    mixins.RetrieveModelMixin, ## 提供处理 GET 请求获取单个对象的方法
    mixins.UpdateModelMixin,
    mixins.DestroyModelMixin,
    mixins.ListModelMixin,
    generics.GenericAPIView  ## DRF基类 处理不同 http 请求
):
    queryset = Merchant.objects.all()
    serializer_class = MerchantSerializer

    def get(self,request,pk=None):
        if pk:
            return self.retrieve(request)
        else:
            return self.list(request)

    def post(self,request):
        return self.create(request)

    def put(self,request,pk=None):
        return self.update(request,pk)

    def delete(self,request,pk):
        return self.destroy(request,pk)